package com.sansecy.customview.view.check;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class CheckedView extends View {
    private Paint mPaint;
    private Path mDstPath;
    private Path mCirclePath;
    private PathMeasure mPathMeasure;
    private boolean isChecked = false;
    private int mW;
    private int mH;
    private int mBorderColor = Color.parseColor("#547fdd");
    RectF rectF = new RectF();

    public CheckedView(Context context) {
        this(context, null);
    }

    public CheckedView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.BLACK);
        mPaint.setAntiAlias(true);
        mDstPath = new Path();
        mCirclePath = new Path();
        mPathMeasure = new PathMeasure();
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mW = w;
        mH = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(mW / 2, mH / 2);
        mPathMeasure.getSegment(0, mPathMeasure.getLength(), mDstPath, true);
        mPaint.setColor(mBorderColor);
        if (isChecked) {
            mPaint.setColor(Color.BLACK);
            mPathMeasure.nextContour();
            float stop = mPathMeasure.getLength();
            mPathMeasure.getSegment(0, stop, mDstPath, true);
        }
        canvas.drawPath(mDstPath, mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureDimension(200, widthMeasureSpec);
        int height = measureDimension(200, heightMeasureSpec);
        setMeasuredDimension(width, height);
        mCirclePath.reset();
        rectF.set(0f, 0f, (float) getRight(), (float) getBottom());
        mCirclePath.addRect(rectF, Path.Direction.CW);

//        mRadius = Math.min(width, height) / 2 - mStrokeWidth;
//        mCirclePath.moveTo(0 - getWidth() / 2, 0);
//        mCirclePath.lineTo(0, mCenterY + mRadius / 2);
//        mCirclePath.lineTo(0 + getWidth() / 2, mCenterY - mRadius / 3);
//        mPathMeasure.setPath(mCirclePath, false);
    }

    public int measureDimension(int defaultSize, int measureSpec) {
        int result;

        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = defaultSize;   //UNSPECIFIED
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

}